<?php
class Register extends MY_Controller{

	function __construct() {
		parent::__construct();
		
		$this->load->library('form_validation');
		$this->load->model('user_model', '', TRUE);
		$this->load->model('society_model', '', TRUE);
		$this->load->model('association_model', '', TRUE);
		$this->load->model('teacher_model', '', TRUE);
		$this->load->model('config_model', '', TRUE);
	}


	/**
	 * 综合注册应用处理
	 */
	function index() {
		$data['current_menu_tab'] = MENU_TAB_A;
		$data['html_title'] = '用户注册';
		
		//注册标识类型
		$category_id = $this->input->post("category_id");

		//常规公共校验
		$this->_validate_common();
		
		switch ($category_id) {
			case SOCIETY_UCID:  //社团校验
				$this->_validate_society();
				break;
			case ASSOCIATION_UCID:  //社联成员校验
				$this->_validate_association();
				break;
		}
		
		//开始常规验证
		if ($this->form_validation->run() == FALSE) {
			$data['temp'] = $this->_get_form_array();
			
			$this->load->view('frontend/register', $data);
			return;
		}
		
		//逻辑校验
		$this->form_validation->set_rules('c_name_duplicate_error', '', 'callback_name_check');
		$this->form_validation->set_rules('c_email_duplicate_error', '', 'callback_email_check');
		switch ($category_id) {
			case SOCIETY_UCID:
				$this->form_validation->set_rules('s_name_duplicate_error', '', 'callback_s_name_check');
				$this->form_validation->set_rules('s_abbr_duplicate_error', '', 'callback_s_abbr_check');
				$this->form_validation->set_rules('contact_phone_duplicate_error', '', 'callback_contact_phone_check');
				$this->form_validation->set_rules('contact_qq_duplicate_error', '', 'callback_contact_qq_check');
				break;
			case ASSOCIATION_UCID:
				$this->form_validation->set_rules('a_stu_num_duplicate_error', '', 'callback_a_stu_num_check');
				$this->form_validation->set_rules('a_phone_duplicate_error', '', 'callback_a_phone_check');
				$this->form_validation->set_rules('a_qq_duplicate_error', '', 'callback_a_qq_check');
				break;
		}
		
		//开始逻辑校验
		if ($this->form_validation->run() == FALSE) {
			$this->load->view('frontend/register', $data);
			return;
		}
		
		//得到过滤后的user数据
		$user = $this->_get_user_data();
		//设置数据创建时间
		$user['create_time'] = time();
		//保存user数据获得user_id
		$user_id = $this->user_model->save($user);
		
		switch ($category_id) {
			//社团及指导老师的数据处理
			case SOCIETY_UCID:
				
				//往指导老师用户表中存入数据
				switch (strlen($user_id)) {
					case 1:
						$teacher_user['name'] = 'ST00' . $user_id;
						break;
					case 2:
						$teacher_user['name'] = 'ST0' . $user_id;
						break;
					case 3:
						$teacher_user['name'] = 'ST' . $user_id;
						break;
				}
				$teacher_user['password'] = random_string('alnum', 10);
				$teacher_user['category_id'] = TEACHER_UCID;
				$teacher_user['create_time'] = time();
				
				//获取指导老师的uid(teacher_user_id)，并把其值传给society表的s_teacher字段
				$teacher_user_id = $this->user_model->save($teacher_user);

				//往老师表中存入数据
				$teacher = $this->_get_teacher_data();
				$teacher['teacher_id'] = $teacher_user_id;
				$this->teacher_model->save($teacher);
				
				//往社团表中存入数据
				$society = $this->_get_society_data();
				$society['society_id'] = $user_id;
				$society['s_teacher'] = $teacher_user_id;
				$society['about'] = htmlspecialchars($society['about']);
				$this->society_model->save($society);
				
				redirect(SOCIETY_LOGIN_PATH);
				break;
				
			//社联成员数据处理
			case ASSOCIATION_UCID:
				$association = $this->_get_association_data();
				$association['employee_id'] = $user_id;
				$association['a_birth'] = filter_var($association['a_birth'], FILTER_SANITIZE_NUMBER_INT);
				$this->association_model->save($association);
				redirect(ASSOCIATION_LOGIN_PATH);
				break;
			default:
				return FALSE;
		}
	}

	/**
	 * 公用数据常规校验
	 */
	function _validate_common() {
		$this->form_validation->set_rules('name', '用户名', 'required|xss_clean|min_length[2]|max_length[20]');
		$this->form_validation->set_rules('password', '密码', 'required|xss_clean|min_length[6]|max_length[16]');
		$this->form_validation->set_rules('passconf', '确认密码', 'required|xss_clean|matches[password]');
		$this->form_validation->set_rules('email', 'Email', 'required|xss_clean|email');
		$this->form_validation->set_rules('category_id', 'category_id', 'required');
	}

	/**
	 * 社联成员分表单信息常规校验
	 */
	function _validate_association() {
		$this->form_validation->set_rules('a_name', '姓名', 'required|xss_clean');
		$this->form_validation->set_rules('a_gender', '性别', 'required');
		$this->form_validation->set_rules('a_stu_num', '学号', 'required|xss_clean|length[8]|numeric');
		$this->form_validation->set_rules('a_phone', '联系电话', 'required|xss_clean|numeric');
		$this->form_validation->set_rules('a_qq', 'QQ', 'required|xss_clean|numeric|min_length[4]|max_length[15]');
	}

	/**
	 * 社团分表单信息常规校验
	 */
	function _validate_society() {
		$this->form_validation->set_rules('s_name', '社团名称', 'required|xss_clean');
		$this->form_validation->set_rules('abbr', '社团简称', 'required|xss_clean|min_length[4]|max_length[8]');
		$this->form_validation->set_rules('member_num', '社团人数', 'required|numeric');
		$this->form_validation->set_rules('about', '社团简介', 'required|xss_clean|max_length[10000]');
		$this->form_validation->set_rules('web_site', '社团主页', 'url|prep_url|xss_clean|max_length[50]');
		$this->form_validation->set_rules('contact', '社团负责人', 'required|xss_clean|max_length[50]');
		$this->form_validation->set_rules('contact_phone', '社团负责人手机', 'required|xss_clean|max_length[11]|numeric');
		$this->form_validation->set_rules('contact_qq', '社团负责人QQ号', 'required|xss_clean|numeric|min_length[4]|max_length[15]');
		$this->form_validation->set_rules('since', '创办年份', 'required');
		$this->form_validation->set_rules('t_name', '指导老师姓名', 'required|xss_clean');
		$this->form_validation->set_rules('t_phone', '指导老师手机', 'required|xss_clean|max_length[11]|numeric');
	}

	/**
	 * 从提交的大表单中提取c_user表中需要的数据
	 */
	function _get_user_data() {
		$association_fields = $this->association_model->get_table_fields();
		$society_fields = $this->society_model->get_table_fields();
		$teacher_fields = $this->teacher_model->get_table_fields();
		$filter_array = array_merge($association_fields, $society_fields, $teacher_fields);
		array_push($filter_array, "passconf");
		return $this->_get_form_array($filter_array);
	}
	
	/**
	 * 从提交的大表单中提取社联成员表中需要的数据
	 */	
	function _get_association_data() {
		$user_fields = $this->user_model->get_table_fields();
		$society_fields = $this->society_model->get_table_fields();
		$teacher_fields = $this->teacher_model->get_table_fields();
		$filter_array = array_merge($user_fields, $society_fields, $teacher_fields);
		array_push($filter_array, "passconf");
		return $this->_get_form_array($filter_array);
	}
		
	/**
	 * 从提交的大表单中提取社团表中需要的数据
	 */	
	function _get_society_data() {
		$user_fields = $this->user_model->get_table_fields();
		$association_fields = $this->association_model->get_table_fields();
		$teacher_fields = $this->teacher_model->get_table_fields();
		$filter_array = array_merge($user_fields, $association_fields, $teacher_fields);
		array_push($filter_array, "passconf");
		//先将社团表之外表中的字段全部得到，再放入_get_form_array方法中得到所填表的数据
		
		return $this->_get_form_array($filter_array);
	}
	
	/**
	 * 从提交的大表单中提取老师表中需要的数据
	 */	
	function _get_teacher_data() {
		$user_fields = $this->user_model->get_table_fields();
		$society_fields = $this->society_model->get_table_fields();
		$association_fields = $this->association_model->get_table_fields();
		$filter_array = array_merge($user_fields, $society_fields, $association_fields);
		array_push($filter_array, "passconf");
		//先将社团表之外表中的字段全部得到，再放入_get_form_array方法中得到所填表的数据
		
		return $this->_get_form_array($filter_array);
	}

     
	/**
	 * 公共信息name重复校验的回调函数
	 */	
	function name_check() {
		$name = $this->input->post('name');
		$data = $this->user_model->get_user_by_name_single($name);
		
		if($data) {
			$this->form_validation->set_message('name_check', '用户名已存在');
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
	function ajax_validate_name() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->user_model->get_user_by_name_single($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
	
	/**
	 * 公共信息email重复校验的回调函数
	 */	
	function email_check() {
		$email = $this->input->post('email');
		$data = $this->user_model->get_user_by_email_single($email);
		
		if($data) {
			$this->form_validation->set_message('email_check', '邮箱已存在');
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
	function ajax_validate_email() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->user_model->get_user_by_email_single($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
	
	/**
	 * 社团信息s_name重复校验的回调函数
	 */	
	function s_name_check() {
		if ($_POST['s_name']) {
			$s_name = $this->input->post('s_name');
			$data = $this->society_model->get_user_by_s_name($s_name);
			
			if($data) {
				$this->form_validation->set_message('s_name_check', "社团已存在，<a style='color: yellow; text-decoration: none;' target='_blank' href='#'>点此申诉</a>");
				return FALSE;
			} else {
				return TRUE;
			}
		}
	}
	
	function ajax_validate_s_name() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->society_model->get_user_by_s_name($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
	
	/**
	 * 社团信息abbr重复校验的回调函数
	 */	
	function s_abbr_check() {
		if ($_POST['abbr']) {
			$abbr = $this->input->post('abbr');
			$data = $this->society_model->get_user_by_s_abbr($abbr);
			
			if($data) {
				$this->form_validation->set_message('s_abbr_check', "社团简称与已知社团重复，<a style='color: yellow; text-decoration: none;' target='_blank' href='#'>点此申诉</a>");
				return FALSE;
			} else {
				return TRUE;
			}
		}
	}
	
	function ajax_validate_s_abbr() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->society_model->get_user_by_s_abbr($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
	
	/**
	 * 社团信息phone重复校验的回调函数
	 */
	function contact_phone_check() {
		if ($_POST['contact_phone']) {
			$phone = $this->input->post('contact_phone');
			$data = $this->society_model->get_user_by_phone($phone);
			
			if($data) {
				$this->form_validation->set_message('contact_phone_check', '手机号码已存在');
				return FALSE;
			} else {
				return TRUE;
			}
		}
	}
	
	function ajax_validate_contact_phone() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->society_model->get_user_by_phone($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
	
	/**
	 * 社团信息qq重复校验的回调函数
	 */
	function contact_qq_check() {
		if ($_POST['contact_qq']) {
			$qq = $this->input->post('contact_qq');
			$data = $this->society_model->get_user_by_qq($qq);
			
			if($data) {
				$this->form_validation->set_message('contact_qq_check', 'QQ号码已存在');
				return FALSE;
			} else {
				return TRUE;
			}
		}
	}
	
	function ajax_validate_contact_qq() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->society_model->get_user_by_qq($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
	
	/**
	 * 社联成员信息a_stu_num重复校验的回调函数
	 */	
	function a_stu_num_check() {
		if ($_POST['a_stu_num']) {
			$stu_num = $this->input->post('a_stu_num');
			$data = $this->association_model->get_user_by_stu_num($stu_num);
			
			if($data) {
				$this->form_validation->set_message('a_stu_num_check', '学号已存在');
				return FALSE;
			} else {
				return TRUE;
			}
		}
	}
	
	function ajax_validate_a_stu_num() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->association_model->get_user_by_stu_num($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
	
	/**
	 * 社联成员信息phone重复校验的回调函数
	 */
	function a_phone_check() {
		if ($_POST['a_phone']) {
			$phone = $this->input->post('a_phone');
			$data = $this->association_model->get_user_by_phone($phone);
			
			if($data) {
				$this->form_validation->set_message('a_phone_check', '手机号码已存在');
				return FALSE;
			} else {
				return TRUE;
			}
		}
	}
	
	function ajax_validate_a_phone() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->association_model->get_user_by_phone($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
	
	/**
	 * 社联成员信息qq重复校验的回调函数
	 */
	function a_qq_check() {
		if ($_POST['a_qq']) {
			$qq = $this->input->post('a_qq');
			$data = $this->association_model->get_user_by_qq($qq);
			
			if($data) {
				$this->form_validation->set_message('a_qq_check', 'QQ号码已存在');
				return FALSE;
			} else {
				return TRUE;
			}
		}
	}
	
	function ajax_validate_a_qq() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		if (!$this->association_model->get_user_by_qq($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}	
		}
	}
			
	/**
	 * 得到表单数据
	 * @param $unset 过滤数据 可以是字符串 也可以是数组
	 */
	function _get_form_array($unset = "") {
		$form_data = array();

		foreach ($_POST as $k => $v) {

			if (is_array($unset)) {
				if (!in_array($k, $unset)) {
					$form_data[$k] =  $v;
				}
			} else {
				if ($k != $unset) {
					$form_data[$k] =  $v;
				}
			}

		}
		return $form_data;
	}

}